home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / The Hacks / NotEnoughSecrets / Secret About Box PB / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-06-23  |  8.0 KB  |  356 lines

  1. #include <stdlib.h>
  2. #include <math.h>
  3.  
  4. #include <Carbon/Carbon.h>
  5.  
  6. void SetRandomBackColor();
  7. void CenterWindow(Rect* theRect);
  8. void StartBoard(WindowRef theWindow);
  9. void HandlePaddle();
  10. void HandleBall();
  11. short TheStringWidth(Str255 theString);
  12.  
  13. double gBallVelX;
  14. double gBallVelY;
  15.  
  16. double gBallPosX;
  17. double gBallPosY;
  18.  
  19. Rect *gWords;
  20. short gNumWords=0;
  21.  
  22.  
  23.  
  24. Rect gPaddleRect;
  25.  
  26. short gPaddleVel;
  27.  
  28. void main()
  29.     WindowRef theWindow;
  30.     Rect windowRect={0,0,300,400};
  31.     long t;
  32.     EventRecord theEvent;
  33.     
  34.     gBallPosX = 100;
  35.     gBallPosY = 250;
  36.     gBallVelX = 1.1;
  37.     gBallVelY = -2.4;
  38.     
  39.     CenterWindow(&windowRect);
  40.     
  41.     theWindow = NewWindow(0,&windowRect,"\p",true,1,(WindowRef)-1L,0,0);
  42.     SetQDGlobalsRandomSeed(TickCount());
  43.         
  44.     StartBoard(theWindow);
  45.     
  46.     ForeColor(blackColor);
  47.     BackColor(whiteColor);
  48.     
  49.     while (!Button())
  50.     {
  51.         t = TickCount();
  52.     
  53.         HandlePaddle();
  54.         HandleBall();
  55.         
  56.         while (t==TickCount())
  57.         {
  58.             WaitNextEvent(everyEvent,&theEvent,1,0);
  59.         }
  60.     }
  61.     DisposeWindow(theWindow);
  62.     DisposePtr((Ptr)gWords);
  63.         
  64. }
  65.  
  66.  
  67. void SetRandomBackColor()
  68. {
  69.     switch (abs(Random())%7)
  70.     {
  71.         case 0:
  72.             BackColor(blackColor);
  73.             break;
  74.         case 1:
  75.             BackColor(redColor);
  76.             break;
  77.         case 2:
  78.             BackColor(greenColor);
  79.             break;
  80.         case 3:
  81.             BackColor(blueColor);
  82.             break;
  83.         case 4:
  84.             BackColor(cyanColor);
  85.             break;
  86.         case 5:
  87.             BackColor(magentaColor);
  88.             break;
  89.         case 6:
  90.             BackColor(yellowColor);
  91.             break;
  92.     }
  93. }
  94.  
  95. void CenterWindow(Rect* theRect)
  96. {
  97.     GrafPtr thePort;
  98.     Rect screenRect;
  99.     short    leftPos,topPos,rectWidth,rectHeight;
  100.     
  101.     GetPort(&thePort);
  102.     GetPortBounds(thePort,&screenRect);
  103.  
  104.     leftPos = ((screenRect.right - screenRect.left) - (theRect->right - 
  105.                 theRect->left)) / 2;
  106.     topPos = ((screenRect.bottom - screenRect.top) 
  107.                 - (theRect->bottom - theRect->top))/2;
  108.  
  109.     rectWidth = theRect->right - theRect->left;
  110.     rectHeight = theRect->bottom - theRect->top;
  111.     
  112.     theRect->top = topPos;
  113.     theRect->bottom = theRect->top + rectHeight;
  114.     theRect->left = leftPos;
  115.     theRect->right = theRect->left + rectWidth;
  116. }
  117.  
  118. void StartBoard(WindowRef theWindow)
  119. {
  120.     Rect windowRect;
  121.     FontInfo fInfo;
  122.     Handle rHandle;
  123.     short numStrings;
  124.     Str255 theString;
  125.     Str255 wordString;
  126.     short cursor,oldCursor;
  127.     short vPos,a;
  128.     Rect blockRect;
  129.     char *stringsUsed;
  130.         short stringToUse;
  131.     short stringsToUseOnLine[8];
  132.         short stringsToUseOnLineIndex;
  133.     short lineWidth;
  134.         float bufferPixels;
  135.         
  136.     rHandle = GetResource('STR#',128);
  137.     if (!rHandle)
  138.         ExitToShell();
  139.         
  140.     BlockMove(*rHandle,&numStrings,sizeof(short));
  141.     ReleaseResource (rHandle);
  142.     
  143.     HideCursor();
  144.     
  145.     stringsUsed = NewPtr(numStrings);
  146.     
  147.     for  (a=0;a<numStrings;a++)
  148.         stringsUsed[a]=0;
  149.     
  150.     SetPort(GetWindowPort(theWindow));
  151.     GetPortBounds(GetWindowPort(theWindow),&windowRect);
  152.     
  153.     OffsetRect(&windowRect,-windowRect.left,-windowRect.top);
  154.     
  155.     ForeColor(whiteColor);
  156.     TextSize(10);
  157.     TextFace(bold);
  158.     GetFontInfo(&fInfo);
  159.     
  160.     blockRect.left = 0;
  161.     blockRect.right = 0;
  162.     vPos = 40;
  163.     lineWidth=0;
  164.         stringsToUseOnLineIndex=0;
  165.         
  166.     gWords = (Rect*)NewPtr(sizeof(Rect)*500);  //Hack, but it should be enough
  167.     
  168.     while (vPos<12*8+40)
  169.     {
  170.         while(1)
  171.                 {
  172.                     do {
  173.                         stringToUse = abs(Random())%numStrings;
  174.                     }while(stringsUsed[stringToUse]);
  175.                     
  176.                     
  177.                     GetIndString(theString,128,stringToUse);
  178.                     
  179.                     if (TheStringWidth(theString)+lineWidth<400)
  180.                     {
  181.                         lineWidth+=TheStringWidth(theString);
  182.                         stringsToUseOnLine[stringsToUseOnLineIndex] = stringToUse;
  183.                         stringsToUseOnLineIndex++;
  184.                         stringsUsed[stringToUse]=1;
  185.                     }
  186.                     else
  187.                         break;
  188.                 }
  189.                 bufferPixels = (400-lineWidth)/(stringsToUseOnLineIndex-1);
  190.                 
  191.                 for (a=0;a<stringsToUseOnLineIndex;a++)
  192.                 {
  193.                     SetRandomBackColor();
  194.                     
  195.                     cursor = 0;
  196.                     oldCursor = 1;
  197.                     
  198.                     GetIndString(theString,128,stringsToUseOnLine[a]);
  199.                     
  200.                     while (cursor<=theString[0])
  201.                     {
  202.                             for (cursor=oldCursor;theString[cursor]!=' ' && cursor<=theString[0];cursor++)
  203.                                     ;
  204.                             
  205.                             BlockMove(&(theString[oldCursor]),&(wordString[1]),cursor-oldCursor);
  206.                             wordString[0] = (char)((char)cursor-(char)oldCursor);
  207.                             
  208.                             blockRect.top=vPos-fInfo.ascent-fInfo.descent;
  209.                             blockRect.bottom = vPos;
  210.                             if (blockRect.right)
  211.                                 blockRect.left = blockRect.right+2;
  212.                             blockRect.right = StringWidth(wordString)+blockRect.left;
  213.                             
  214.                             gWords[gNumWords] = blockRect;
  215.                             gNumWords++;
  216.                             
  217.                             MoveTo(blockRect.left,vPos-fInfo.descent);
  218.                             EraseRect(&blockRect);
  219.                             DrawString(wordString);
  220.                             
  221.                             oldCursor = cursor+1;
  222.                     }
  223.                     blockRect.right+=bufferPixels;
  224.                 }
  225.         
  226.                 blockRect.right=0;
  227.                 blockRect.left=2;
  228.                 vPos+=16;
  229.                 lineWidth=0;
  230.                 stringsToUseOnLineIndex=0;
  231.     }
  232.     
  233.     ShowCursor();
  234.     
  235.     //gNumWords--;
  236.     DisposePtr(stringsUsed);
  237. }
  238.  
  239. void HandlePaddle()
  240. {
  241.     Point mouseLoc;
  242.     static Rect oldPaddleRect;
  243.     Rect removeRect;
  244.     
  245.     //Handle Paddle
  246.     GetMouse(&mouseLoc);
  247.     
  248.     if (mouseLoc.h<0)
  249.         gPaddleRect.left = 0;
  250.     else
  251.         gPaddleRect.left = mouseLoc.h;
  252.     
  253.     gPaddleRect.right = gPaddleRect.left+25;
  254.     if (gPaddleRect.right>400)
  255.     {
  256.         gPaddleRect.right = 400;
  257.         gPaddleRect.left = 370;
  258.     }
  259.     gPaddleRect.top = 300-32;
  260.     gPaddleRect.bottom = gPaddleRect.top+4;
  261.     
  262.     PaintRect(&gPaddleRect);
  263.     
  264.     gPaddleVel = gPaddleRect.right-oldPaddleRect.right;
  265.     if (gPaddleVel>5)
  266.         gPaddleVel=5;
  267.     if (gPaddleVel<-5)
  268.         gPaddleVel=-5;
  269.         
  270.     if (oldPaddleRect.right > gPaddleRect.right)
  271.     {
  272.         removeRect.left = gPaddleRect.right;
  273.         removeRect.right = oldPaddleRect.right;
  274.     }
  275.     else
  276.     {
  277.         removeRect.left = oldPaddleRect.left;
  278.         removeRect.right = gPaddleRect.left;
  279.     }
  280.     removeRect.top = oldPaddleRect.top;
  281.     removeRect.bottom = oldPaddleRect.bottom;    
  282.     
  283.     EraseRect(&removeRect);
  284.     
  285.     oldPaddleRect = gPaddleRect;
  286. }
  287.     
  288. void HandleBall()
  289. {
  290.     Rect ballRect;
  291.     Rect oldBallRect;
  292.     short a;
  293.     
  294.     oldBallRect.left = gBallPosX-3;
  295.     oldBallRect.right = gBallPosX+3;
  296.     oldBallRect.top = gBallPosY-3;
  297.     oldBallRect.bottom = gBallPosY+3;
  298.     
  299.     gBallPosX += gBallVelX;
  300.     gBallPosY += gBallVelY;
  301.     
  302.     ballRect.left = gBallPosX-3;
  303.     ballRect.right = gBallPosX+3;
  304.     ballRect.top = gBallPosY-3;
  305.     ballRect.bottom = gBallPosY+3;
  306.     
  307.     EraseRect(&oldBallRect);
  308.     PaintOval(&ballRect);
  309.     
  310.     for (a=0;a<gNumWords;a++)
  311.     {
  312.         if (SectRect(&ballRect,&gWords[a],0))
  313.         {
  314.             //Hit a block.  Erase it and bounce the ball
  315.             EraseRect(&gWords[a]);
  316.             
  317.             gWords[a].top=0;
  318.             gWords[a].left=0;
  319.             gWords[a].right=0;
  320.             gWords[a].bottom=0;
  321.             
  322.             gBallVelY =- gBallVelY;
  323.                         break;
  324.         }
  325.     }
  326.     if (SectRect(&gPaddleRect,&ballRect,0))
  327.     {
  328.         //Hit the paddle. bounce the ball
  329.         gBallVelX += (gBallPosX-(gPaddleRect.left+12))/12;
  330.         gBallVelY =- gBallVelY;
  331.     }
  332.     if (ballRect.right>=400 || ballRect.left<=0) //Hit side walls
  333.         gBallVelX =- gBallVelX;
  334.         
  335.     if (ballRect.top<=0)    //Hit top wall
  336.         gBallVelY =- gBallVelY;
  337.     if (ballRect.bottom>=400)
  338.     {
  339.         gBallVelX = ((float)Random()/(float)20000)+2;
  340.         gBallVelY = ((float)Random()/(float)20000)+2;
  341.         gBallPosX = abs(Random())%300;
  342.         gBallPosY = abs(Random())%100+100;
  343.     }
  344. }
  345.  
  346. short TheStringWidth(Str255 theString)
  347. {
  348.     char a;
  349.     short additionalWidth=0;
  350.     
  351.     for (a=0;a<theString[0];a++)
  352.         if (theString[a+1]==' ')
  353.             additionalWidth+=2;
  354.     return StringWidth(theString)+additionalWidth;
  355. }